/*
 * Created by SharpDevelop.
 * User: dvetters
 * Date: 9/24/2010
 * Time: 12:16 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Echium2
{
	/// <summary>
	/// A panel that dsplays the info on the currently playing song
	/// </summary>
	public partial class MediaInfoPane : Panel
	{
		private bool dragging = false;
		private double m_position;
		private EchiumTrack playingTrack;
		
		public event EventHandler<EventArgs<double>> PositionManuallyChangedEvent;
		
		public string Artist
		{
			get { return this.playingTrack.Artist; }
		}
		
		public string Album
		{
			get { return this.playingTrack.Album; }
		}
		
		public string Title
		{
			get { return this.playingTrack.Title; }
		}
		
		public double Position
		{
			get { return this.m_position; }
			set
			{
				this.m_position = value;
				this.Invalidate();
			}
		}
		
		public MediaInfoPane()
		{
			InitializeComponent();
		}
		
		public void SetMediaInfo(EchiumTrack track)
		{
			this.playingTrack = track;
			this.Invalidate();
		}
		
		private void MediaInfoPane_MouseMove(object sender, MouseEventArgs e)
		{
			if (this.dragging)
			{
				this.Invalidate();
			}
		}
		
		private void MediaInfoPane_MouseUp(object sender, MouseEventArgs e)
		{
			if (this.dragging)
			{
				this.dragging = false;
				int position = Cursor.Position.X - this.Left - this.Parent.Left - 15;
				if (playingTrack != null)
					this.PositionManuallyChangedEvent.Invoke(this, new EventArgs<double>(((double)position/(double)(this.DisplayRectangle.Width-15)) * (double)playingTrack.Duration));
				else this.Invalidate();
			}
		}
		
		private void MediaInfoPane_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Left)
			{
				if (e.X >= 8 && e.X <= this.DisplayRectangle.Width - 10 && e.Y >= 8 && e.Y <= 18)
				{
					this.dragging = true;
					this.Invalidate();
				}
			}
		}
		
		protected override void OnPaintBackground(PaintEventArgs e)
		{
			Brush backgroundBrush = new LinearGradientBrush(e.ClipRectangle, Color.Gray, Color.LightGray, LinearGradientMode.Vertical);
			e.Graphics.FillRectangle(backgroundBrush, this.DisplayRectangle);
			//why i have to say this.DisplayRectangle.Width - 10 instead of this.DisplayRectangle.Width - 5 here is beyond me
			e.Graphics.DrawRectangle(new Pen(Color.FloralWhite), 5, 5, this.DisplayRectangle.Width - 10, 15);
		}
		
		protected override void OnPaint(PaintEventArgs e)
		{
			//base.OnPaint(e);
			#region progress meter
			Brush progressBrush = new SolidBrush(Color.FloralWhite);
			if (!this.dragging)
			{
				if (this.playingTrack != null && this.playingTrack.Duration != 0)
				{
					e.Graphics.FillRectangle(progressBrush, 8, 8, (int)((this.m_position / this.playingTrack.Duration) * (this.DisplayRectangle.Width - 15)), 10);
				}
				else
				{
					this.m_position = 0;
					e.Graphics.FillRectangle(new SolidBrush(Color.Transparent), 8, 8, this.DisplayRectangle.Width - 15, 10);
				}
			}
			else
			{
				e.Graphics.FillRectangle(progressBrush, 8, 8, Math.Min(Cursor.Position.X - this.Left - this.Parent.Left - 15, this.DisplayRectangle.Width - 15), 10);
			}
			#endregion
			if (this.playingTrack != null)
			{
				#region time remaining
				Font timeDisplayFont = new Font(FontFamily.GenericMonospace, 10, FontStyle.Regular, GraphicsUnit.Point);
				e.Graphics.DrawString(FormatTimeSpan(TimeSpan.FromMilliseconds(m_position)), timeDisplayFont, progressBrush, 8, 20);
				string durationTimeString = FormatTimeSpan(TimeSpan.FromMilliseconds(this.playingTrack.Duration));
				e.Graphics.DrawString(durationTimeString, timeDisplayFont, progressBrush, 
				                      this.DisplayRectangle.Width - 10 - e.Graphics.MeasureString(durationTimeString, timeDisplayFont).Width, 20);
				#endregion
				#region track info
				Font trackInfoFont = new Font(FontFamily.GenericSansSerif, 16, FontStyle.Regular, GraphicsUnit.Point);
				string trackInfoString = this.Artist + " - " + ((!string.IsNullOrEmpty(this.Album))?this.Album + " - ":"") + this.Title;
				e.Graphics.DrawString(trackInfoString, trackInfoFont, new SolidBrush(Color.Black),
				                      (this.DisplayRectangle.Width -10)/2 - e.Graphics.MeasureString(trackInfoString, trackInfoFont).Width/2, 
				                      (this.DisplayRectangle.Height - (this.DisplayRectangle.Height - 15)/2) - trackInfoFont.Height/2);
				#endregion
			}
		}
		
		protected override void OnSizeChanged(EventArgs e)
		{
			base.OnSizeChanged(e);
			this.Invalidate();
		}
		
		#region Utilities
		private string FormatTimeSpan(TimeSpan span)
		{
			string ret = "";
			if (span.Hours > 0) ret += span.Hours.ToString() + ":";
			if (span.Minutes < 10) ret += "0";
			ret += span.Minutes + ":";
			if (span.Seconds < 10) ret += "0";
			ret += span.Seconds;
			return ret;
		}
		#endregion
	}
}
